#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>

int main(){
	int pipedf[2];
	int res=0;
	int byte=0;
	char buf[256];
	if(res==-1){
		perror("pipe");
		exit(1);
	}
	while(1){
		res=pipe(pipedf);
		res=fork();
		if(res==-1){
			perror("fork");
			exit(1);
		}
		if(res==0){
			close(pipedf[1]);
			memset(buf,0,sizeof(buf));
			byte=read(pipedf[0],buf,sizeof(buf));
			if(byte==-1){
				perror("read");
				exit(1);
			}
			write(0,buf,strlen(buf));
			putchar('\n');
			close(pipedf[0]);
			exit(0);
		}else{
			close(pipedf[0]);
			memset(buf,0,sizeof(buf));
			write(0,"===>",5);
			read(0,buf,sizeof(buf));
			buf[strlen(buf)-1]='\0';
			if(strcmp("exit",buf)==0){
				close(pipedf[1]);
				exit(0);
			}
			res=write(pipedf[1],buf,strlen(buf));
			if(res==-1){
				perror("write");
				close(pipedf[1]);
				exit(1);
			}
			wait(NULL);
			close(pipedf[1]);
		}
	}
	return 0;
}
